//
//  MainViewController.swift
//  WeiBo(Swift)
//
//  Created by 周成龙 on 16/9/18.
//  Copyright © 2016年 ZCL. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    // MARK: - 懒加载
    lazy var composeBtn: UIButton = {
        let btn = UIButton(imageName: "tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
        
        btn.addTarget(self, action: #selector(MainViewController.composeBtnClick), for: UIControlEvents.touchUpInside)
        
        return btn
    }()
    
    // 添加 @objc private 使 composeBtnClick 方法私有化
    @objc private func composeBtnClick() {
        ZLog(message: "------")
    }
    
    // MARK: - 生命周期
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tabBar.tintColor = UIColor.orange
        addChildViewControllers()
        
    }
    
    // 添加 加号 按钮
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        tabBar.addSubview(composeBtn)
        let rect = composeBtn.frame
        let width = tabBar.bounds.width / CGFloat((tabBar.items?.count)!)
        composeBtn.frame = CGRect(x: 2*width, y: 0, width: width, height: rect.height)
    }
    
    // MARK: - 内部方法
    private func addChildViewControllers() {
        
        addChildViewController(vc: HomeTableViewController(), imageName: "tabbar_home", title: "首页")
        addChildViewController(vc: MessageTableViewController(), imageName: "tabbar_message_center", title: "消息")
        addChildViewController(vc: NullTableViewController(), imageName: "", title: "")
        addChildViewController(vc: DiscoveryTableViewController(), imageName: "tabbar_discover", title: "发现")
        addChildViewController(vc: ProfileTableViewController(), imageName: "tabbar_profile", title: "我的")
    }
    
    private  func addChildViewController(vc: UIViewController, imageName: String, title: String) {
        
        let nav = UINavigationController(rootViewController: vc)
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName+"_highlighted")
        
        addChildViewController(nav)
    }
}
